<?php

$QUIZ_TABLE = "quiz";
$CATEGORY_TABLE = "category";
$PDO = getPDO();

// returns a PDO object
function getPDO() {
    $host = '127.0.0.1';    # TO COMPLETE
    $db   = 'quiz';
    $user = 'root';    # TO COMPLETE
    $pass = '111111';    # TO COMPLETE
    $charset = 'utf8';

    $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
    $options = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];
    try {
        return new PDO($dsn, $user, $pass, $options);
    } catch (\PDOException $e) {
        throw new \PDOException($e->getMessage(), (int)$e->getCode());
    }
}

function select($PDO,$category,$level,$size,$mode2){
    $eq1 = strcmp($category , 'random');
    $eq2 = strcmp($level , 'random');


    $select = "select *  ";
    $from = "from question ";
    $where = " where ";

    if($eq1 != 0){
        $where = $where . " category='$category' and ";
    }
    if($eq2 != 0){
        $where = $where . " level_part = '$level' and ";
    }
    if(strcmp($mode2,"mixed")!=0){
        $where = $where . " ispublic='$mode2' and ";
    }

    $where = $where ." 1=1 ";

    $order = "order by rand() ";
    $limit = "limit $size ";


    $sql = $select . $from . $where  . $order . $limit;

    return $PDO->query($sql);
}


function is_session_started() {
    if ( php_sapi_name() !== 'cli' ) {
        if ( version_compare(phpversion(), '5.4.0', '>=') ) {
            return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
        } else {
            return session_id() === '' ? FALSE : TRUE;
        }
    }
    return FALSE;
}

function insert_into_history($username,$qids,$answers,$mode,$limit,$PDO){

    $sql = "SELECT uid from users where username=?";
    $sth = $PDO ->prepare($sql);
    $sth->bindParam(1,$username);
    $sth->execute();
    $res = $sth->fetch();
    $uid = number_format($res["uid"]);

    $size = count($qids);

    $year = date("Y",time());
    $month = number_format(date("n",time()));
    $day = number_format(date("j",time()));
    $hour = number_format(date("H",time()));
    $minute = number_format(date("i",time()));
    $second = number_format(date("s",time()));

    for($i = 0; $i < $size ; $i = $i + 1){
        $sql = "insert into history 
                (uid,qid,user_answer,quiz_mode,time_limit,quiz_year,quiz_month,quiz_day,quiz_hour,quiz_min,quiz_sec) value
                (?,?,?,
                ?,?,?,
                ?,?,?,
                ?,?)";
        $sth = $PDO ->prepare($sql);

        $sth->bindParam(1,$uid);
        $sth->bindParam(2,$qids[$i]);
        $sth->bindParam(3,$answers[$i]);

        $sth->bindParam(4,$mode);
        $sth->bindParam(5,$limit);
        $sth->bindParam(6,$year);
        $sth->bindParam(7,$month);
        $sth->bindParam(8,$day);
        $sth->bindParam(9,$hour);
        $sth->bindParam(10,$minute);
        $sth->bindParam(11,$second);


        $sth->execute();
    }
}

function select_history_by_username($username,$PDO){
    $sql = "select * from users where username = '$username'";
    return $PDO->query($sql);
}

function select_history_by_uid($uid,$PDO){
    $sql = "select * from history where uid=$uid";
    $table_array = [];
    $res = $PDO->query($sql);
    while($row=$res->fetch()){
        array_push($table_array,$row);
    }
    return $table_array;

}
function select_questions_like($category,$level,$answer,$question,$PDO){
    $sql = "select * from question where category like '%$category%' and level_part like '%$level%' and answer_part like '%$answer%' and question_part like '%$question%'";
    $table_array = [];
    $res = $PDO->query($sql);
    while($row=$res->fetch()){
        array_push($table_array,$row);
    }
    return $table_array;
}

function delete_question_by_qid($qid,$PDO)
{
    $sql = "delete from question where qid=$qid";
    $PDO->exec($sql);
}

function select_question_by_qid($qid,$PDO){
    $sql = "select * from question where qid=$qid";
    $res = $PDO->query($sql);
    $row = $res->fetch();
    return $row["question_part"]."&".$row["answer_part"]."&".$row["category"]."&".$row["level_part"];
}
function update_question_by_qid($qid,$question,$answer,$category,$level,$PDO){
    $sql= "update question set qid = $qid, question_part = '$question' ,answer_part = '$answer',category = '$category',level_part = '$level' where qid = $qid";
    $PDO->exec($sql);
}

function insert_category($cname,$PDO){
    $sql = "insert into category
                (cname) value
                (?)";
    $sth = $PDO ->prepare($sql);

    $sth->bindParam(1,$cname);

    $sth->execute();
}

function select_category($PDO){
    $sql = "select * from category";
    $table_array = [];
    $res = $PDO->query($sql);
    while($row=$res->fetch()){
        array_push($table_array,$row);
    }
    return $table_array;

}
function insert_private_question($question,$answer,$category,$level,$uid,$PDO){
    $sql= "insert into question (question_part,answer_part,category,level_part,ispublic,uid) value(?,?,?,?,?,?)";
    $isPublic = "private";
    $sth = $PDO ->prepare($sql);

    $sth->bindParam(1,$question);
    $sth->bindParam(2,$answer);
    $sth->bindParam(3,$category);
    $sth->bindParam(4,$level);
    $sth->bindParam(5,$isPublic);
    $sth->bindParam(6,$uid);

    $sth->execute();
}

function insert_question($question,$answer,$category,$level,$PDO){
    $sql= "insert into question (question_part,answer_part,category,level_part) value(?,?,?,?)";
    $sth = $PDO ->prepare($sql);

    $sth->bindParam(1,$question);
    $sth->bindParam(2,$answer);
    $sth->bindParam(3,$category);
    $sth->bindParam(4,$level);

    $sth->execute();
}
?>
